package com.ruoyi.friend.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.article.domain.Email;
import com.ruoyi.article.service.impl.MailServiceImpl;
import com.ruoyi.web.model.AppResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.friend.domain.SysFriend;
import com.ruoyi.friend.service.ISysFriendService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 友情链接Controller
 * 
 * @author zzzyh
 * @date 2025-09-15
 */
@RestController
@RequestMapping("/friend/friend")
@Slf4j
public class SysFriendController extends BaseController
{
    @Autowired
    private ISysFriendService sysFriendService;
    @Autowired
    private MailServiceImpl mailService;

    /**
     * 查询友情链接列表
     */
    @PreAuthorize("@ss.hasPermi('friend:friend:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysFriend sysFriend)
    {
        startPage();
        List<SysFriend> list = sysFriendService.selectSysFriendList(sysFriend);
        return getDataTable(list);
    }

    /**
     * 导出友情链接列表
     */
    @PreAuthorize("@ss.hasPermi('friend:friend:export')")
    @Log(title = "友情链接", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysFriend sysFriend)
    {
        List<SysFriend> list = sysFriendService.selectSysFriendList(sysFriend);
        ExcelUtil<SysFriend> util = new ExcelUtil<SysFriend>(SysFriend.class);
        util.exportExcel(response, list, "友情链接数据");
    }

    /**
     * 获取友情链接详细信息
     */
    @PreAuthorize("@ss.hasPermi('friend:friend:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(sysFriendService.selectSysFriendById(id));
    }

    /**
     * 新增友情链接
     */
    @PreAuthorize("@ss.hasPermi('friend:friend:add')")
    @Log(title = "友情链接", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysFriend sysFriend)
    {
        return toAjax(sysFriendService.insertSysFriend(sysFriend));
    }

    /**
     * 修改友情链接
     */
    @PreAuthorize("@ss.hasPermi('friend:friend:edit')")
    @Log(title = "友情链接", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysFriend sysFriend)
    {
        return toAjax(sysFriendService.updateSysFriend(sysFriend));
    }

    /**
     * 删除友情链接
     */
    @PreAuthorize("@ss.hasPermi('friend:friend:remove')")
    @Log(title = "友情链接", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sysFriendService.deleteSysFriendByIds(ids));
    }

    @PostMapping("/sendEmail")
    public AppResult<String> sendEmail(@RequestBody Email email){
        log.info("发送邮件: {}", email);
        mailService.sendCodeEmail(email);
        return AppResult.success("发送成功");
    }
}
